package com.lefevre.cms.web.action.theme;

import com.lefevre.cms.bean.theme.Forum;
import com.lefevre.cms.bean.theme.Layout;
import com.lefevre.cms.service.theme.ThemeService;
import com.lefevre.cms.utils.threadLocal.TemplateThreadLocal;
import com.lefevre.cms.web.action.AccessDeviceUtil;
import com.lefevre.cms.web.action.theme.impl.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模板管理入口
 *
 * @author Lefevre
 */
@Component("templateMain")
public class TemplateMain {
    @Resource
    ThemeService themeService;
    @Resource
    AccessDeviceUtil accessDeviceUtil;

    @Resource
    Tag_TemplateManage tag_TemplateManage;//标签 -- 模板方法实现
    @Resource
    Topic_TemplateManage topic_TemplateManage;//话题 -- 模板方法实现
    @Resource
    QuestionTag_TemplateManage questionTag_TemplateManage;// 问题标签 -- 模板方法实现
    @Resource
    Question_TemplateManage question_TemplateManage;// 问题 -- 模板方法实现
    @Resource
    Feedback_TemplateManage feedback_TemplateManage;
    @Resource
    Links_TemplateManage links_TemplateManage;// 友情链接 -- 模板方法实现

    @Resource
    MembershipCard_TemplateManage membershipCard_TemplateManage;// 会员卡 -- 模板方法实现

    @Resource
    Column_TemplateManage column_TemplateManage;// 站点栏目 -- 模板方法实现

    @Resource
    Help_TemplateManage help_TemplateManage;//在线帮助 -- 模板方法实现
    @Resource
    Advertising_TemplateManage advertising_TemplateManage;//广告 -- 模板方法实现

    @Resource
    Favorite_TemplateManage favorite_TemplateManage;//收藏夹 -- 模板方法实现
    @Resource
    Like_TemplateManage like_TemplateManage;//点赞 -- 模板方法实现
    @Resource
    Follow_TemplateManage follow_TemplateManage;//关注 -- 模板方法实现

    @Resource
    CustomForum_TemplateManage customForum_TemplateManage;//自定义版块 -- 模板方法实现
    @Resource
    System_TemplateManage system_TemplateManage;//系统部分 -- 模板方法实现

    @Resource
    RedEnvelope_TemplateManage redEnvelope_TemplateManage;//红包 -- 模板方法实现

    /**
     * 公共模板处理
     *
     * @param type       布局类型
     * @param layoutFile 布局文件
     */
    public Map<String, Object> list(Integer type, String layoutFile, HttpServletRequest request) throws Exception {
        Map<String, Object> root = new HashMap<>();
        String dirName = themeService.findTemplateDir_cache();//当前模板使用的目录

        //如果引用代码相同，只运行一次
        List<String> referenceCode = new ArrayList<>();
        List<Forum> list = themeService.findForum_cache(dirName, type, layoutFile);
        for (Forum forum : list) {
            if (!referenceCode.contains(forum.getReferenceCode())) {
                //结构   由|线作分割符 " 引用名称 | 引用路径"组成
                root.put(forum.getReferenceCode(), forum.getReferenceCode() + "|templates/" + dirName
                        + "/" + accessDeviceUtil.accessDevices(request) + "/forum/" + forum.getModule() + ".ftl");
            }
            referenceCode.add(forum.getModule());
        }
        return root;
    }

    /**
     * 版块对应的模板对象
     *
     * @param submitParameter  提交参数
     * @param runtimeParameter 运行时参数
     * @return
     */
    public Object templateObject(Forum forum, Map<String, Object> submitParameter, Map<String, Object> runtimeParameter) {
        switch (forum.getForumChildType()) {
            case "标签列表":
                if (forum.getDisplayType().equals("collection")) {
                    return tag_TemplateManage.tag_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "话题列表":
                if (forum.getDisplayType().equals("page")) {
                    return topic_TemplateManage.topic_page(forum, submitParameter, runtimeParameter);
                }
                break;
            case "相似话题":
                if (forum.getDisplayType().equals("collection")) {
                    return topic_TemplateManage.topic_like_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "话题取消隐藏":
                if (forum.getDisplayType().equals("collection")) {
                    return topic_TemplateManage.topicUnhide_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "话题内容":
                if (forum.getDisplayType().equals("entityBean")) {
                    return topic_TemplateManage.content_entityBean(forum, submitParameter, runtimeParameter);
                }
                break;
            case "添加话题":
                if (forum.getDisplayType().equals("collection")) {
                    return topic_TemplateManage.addTopic_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "修改话题":
                if (forum.getDisplayType().equals("collection")) {
                    return topic_TemplateManage.editTopic_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "评论列表":
                if (forum.getDisplayType().equals("page")) {
                    return topic_TemplateManage.comment_page(forum, submitParameter, runtimeParameter);
                }
                break;
            case "添加评论":
                if (forum.getDisplayType().equals("collection")) {
                    return topic_TemplateManage.addComment_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "引用评论":
                if (forum.getDisplayType().equals("collection")) {
                    return topic_TemplateManage.quoteComment_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "修改评论":
                if (forum.getDisplayType().equals("collection")) {
                    return topic_TemplateManage.editComment_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "回复评论":
                if (forum.getDisplayType().equals("collection")) {
                    return topic_TemplateManage.replyComment_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "修改评论回复":
                if (forum.getDisplayType().equals("collection")) {
                    return topic_TemplateManage.editCommentReply_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "问题标签列表":
                if (forum.getDisplayType().equals("collection")) {
                    return questionTag_TemplateManage.questionTag_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "问题列表":
                if (forum.getDisplayType().equals("page")) {
                    return question_TemplateManage.question_page(forum, submitParameter, runtimeParameter);
                }
                break;
            case "问题内容":
                if (forum.getDisplayType().equals("entityBean")) {
                    return question_TemplateManage.content_entityBean(forum, submitParameter, runtimeParameter);
                }
                break;
            case "答案列表":
                if (forum.getDisplayType().equals("page")) {
                    return question_TemplateManage.answer_page(forum, submitParameter, runtimeParameter);
                }
                break;
            case "添加问题":
                if (forum.getDisplayType().equals("collection")) {
                    return question_TemplateManage.addQuestion_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "追加问题":
                if (forum.getDisplayType().equals("collection")) {
                    return question_TemplateManage.appendQuestion_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "添加答案":
                if (forum.getDisplayType().equals("collection")) {
                    return question_TemplateManage.addAnswer_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "修改答案":
                if (forum.getDisplayType().equals("collection")) {
                    return question_TemplateManage.editAnswer_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "回复答案":
                if (forum.getDisplayType().equals("collection")) {
                    return question_TemplateManage.replyAnswer_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "修改答案回复":
                if (forum.getDisplayType().equals("collection")) {
                    return question_TemplateManage.editAnswerReply_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "采纳答案":
                if (forum.getDisplayType().equals("collection")) {
                    return question_TemplateManage.adoptionAnswer_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "回答总数":
                if (forum.getDisplayType().equals("entityBean")) {
                    return question_TemplateManage.answerCount_entityBean(forum, submitParameter, runtimeParameter);
                }
                break;
            case "相似问题":
                if (forum.getDisplayType().equals("collection")) {
                    return question_TemplateManage.question_like_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "加入收藏夹":
                if (forum.getDisplayType().equals("collection")) {
                    return favorite_TemplateManage.addFavorite_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "话题会员收藏总数":
                if (forum.getDisplayType().equals("entityBean")) {
                    return favorite_TemplateManage.favoriteCount_entityBean(forum, submitParameter, runtimeParameter);
                }
                break;
            case "用户是否已经收藏话题":
                if (forum.getDisplayType().equals("entityBean")) {
                    return favorite_TemplateManage.alreadyCollected_entityBean(forum, submitParameter, runtimeParameter);
                }
                break;
            case "问题会员收藏总数":
                if (forum.getDisplayType().equals("entityBean")) {
                    return favorite_TemplateManage.questionFavoriteCount_entityBean(forum, submitParameter, runtimeParameter);
                }
                break;
            case "用户是否已经收藏问题":
                if (forum.getDisplayType().equals("entityBean")) {
                    return favorite_TemplateManage.alreadyFavoriteQuestion_entityBean(forum, submitParameter, runtimeParameter);
                }
                break;
            case "给话题点赞":
                if (forum.getDisplayType().equals("collection")) {
                    return like_TemplateManage.addLike_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "话题点赞总数":
                if (forum.getDisplayType().equals("entityBean")) {
                    return like_TemplateManage.likeCount_entityBean(forum, submitParameter, runtimeParameter);
                }
                break;
            case "用户是否已经点赞该话题":
                if (forum.getDisplayType().equals("entityBean")) {
                    return like_TemplateManage.alreadyLiked_entityBean(forum, submitParameter, runtimeParameter);
                }
                break;
            case "关注用户":
                if (forum.getDisplayType().equals("collection")) {
                    return follow_TemplateManage.addFollow_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "关注总数":
                if (forum.getDisplayType().equals("entityBean")) {
                    return follow_TemplateManage.followCount_entityBean(forum, submitParameter, runtimeParameter);
                }
                break;
            case "粉丝总数":
                if (forum.getDisplayType().equals("entityBean")) {
                    return follow_TemplateManage.followerCount_entityBean(forum, submitParameter, runtimeParameter);
                }
                break;
            case "是否已经关注该用户":
                if (forum.getDisplayType().equals("entityBean")) {
                    return follow_TemplateManage.following_entityBean(forum, submitParameter, runtimeParameter);
                }
                break;
            case "会员卡列表":
                if (forum.getDisplayType().equals("collection")) {
                    return membershipCard_TemplateManage.membershipCard_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "会员卡内容":
                if (forum.getDisplayType().equals("entityBean")) {
                    return membershipCard_TemplateManage.membershipCardContent_entityBean(forum, submitParameter, runtimeParameter);
                }
                break;
            case "购买会员卡":
                if (forum.getDisplayType().equals("collection")) {
                    return membershipCard_TemplateManage.buyMembershipCard_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "发红包内容":
                if (forum.getDisplayType().equals("entityBean")) {
                    return redEnvelope_TemplateManage.content_entityBean(forum, submitParameter, runtimeParameter);
                }
                break;
            case "领取红包用户列表":
                if (forum.getDisplayType().equals("page")) {
                    return redEnvelope_TemplateManage.receiveRedEnvelopeUser_page(forum, submitParameter, runtimeParameter);
                }
                break;
            case "抢红包":
                if (forum.getDisplayType().equals("collection")) {
                    return redEnvelope_TemplateManage.addReceiveRedEnvelope_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "添加在线留言":
                if (forum.getDisplayType().equals("collection")) {
                    return feedback_TemplateManage.addFeedback_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "友情链接列表":
                if (forum.getDisplayType().equals("collection")) {
                    return links_TemplateManage.links_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "图片广告":
                if (forum.getDisplayType().equals("collection")) {
                    return advertising_TemplateManage.recommend_collection_image(forum, submitParameter, runtimeParameter);
                }
                break;
            case "站点栏目列表":
                if (forum.getDisplayType().equals("collection")) {
                    return column_TemplateManage.column_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "在线帮助列表":
                switch (forum.getDisplayType()) {
                    case "monolayer": {//单层
                        return help_TemplateManage.help_monolayer(forum, submitParameter, runtimeParameter);
                    }
                    case "page": {
                        return help_TemplateManage.help_page(forum, submitParameter, runtimeParameter);
                    }
                    case "collection": 
                        return help_TemplateManage.help_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "推荐在线帮助":
                if (forum.getDisplayType().equals("collection")) {
                    return help_TemplateManage.recommend_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "在线帮助分类":
                if (forum.getDisplayType().equals("collection")) {
                    return help_TemplateManage.type_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "在线帮助导航":
                if (forum.getDisplayType().equals("collection")) {
                    return help_TemplateManage.navigation_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "在线帮助内容":
                if (forum.getDisplayType().equals("entityBean")) {
                    return help_TemplateManage.content_entityBean(forum, submitParameter, runtimeParameter);
                }
                break;
            case "用户自定义HTML":
                if (forum.getDisplayType().equals("entityBean")) {
                    return customForum_TemplateManage.customHTML_entityBean(forum, submitParameter, runtimeParameter);
                }
                break;
            case "热门搜索词":
                if (forum.getDisplayType().equals("collection")) {
                    return system_TemplateManage.searchWord_collection(forum, submitParameter, runtimeParameter);
                }
                break;
            case "第三方登录":
                if (forum.getDisplayType().equals("collection")) {
                    return system_TemplateManage.thirdPartyLogin_collection(forum, submitParameter, runtimeParameter);
                }
                break;
        }
        return null;
    }

    /**
     * 公共页(引用版块值)[根据页面是否有标签智能调用]
     *
     * @param quoteTemplate 显示在某个页面上
     */
    public Map<String, Object> publicQuoteCall(String quoteTemplate, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();

        //当前模板使用的目录
        String dirName = themeService.findTemplateDir_cache();
        List<Layout> layoutList = themeService.findLayout_cache(dirName, 6);//公共页(引用版块值)

        for (Layout layout : layoutList) {
            //每个引用代码只含有一个版块
            Forum forum = themeService.findForum_cache(dirName, layout.getReferenceCode());
            if (forum != null) {
                //结构   由|线作分割符 " 引用名称 | 引用路径"组成
                map.put(forum.getReferenceCode(), forum.getReferenceCode() + "|templates/" + dirName + "/" + accessDeviceUtil.accessDevices(request) + "/forum/" + forum.getModule() + ".ftl");
            }
        }
        return map;
    }

    /**
     * 公共页(生成新引用页)[根据页面是否有标签智能调用]
     *
     * @param quoteTemplate 当前引用模板
     */
    public Map<String, Object> newPublic(String quoteTemplate, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        //当前模板使用的目录
        String dirName = themeService.findTemplateDir_cache();
        //检查所有的公共页(生成新引用页)
        List<Layout> publics = themeService.findLayout_cache(dirName, 5);//公共页(生成新引用页)
        for (Layout layout : publics) {
            map.put(layout.getReferenceCode(), layout.getReferenceCode() + "|templates/" + dirName + "/" + accessDeviceUtil.accessDevices(request) + "/public/" + layout.getLayoutFile());
            List<Forum> forumList = themeService.findForumByLayoutId_cache(dirName, layout.getId());
            for (Forum forum : forumList) {
                map.put(forum.getReferenceCode(), forum.getReferenceCode() + "|templates/" + dirName + "/" + accessDeviceUtil.accessDevices(request) + "/forum/" + forum.getModule() + ".ftl");
            }
            //添加到模板参数线程变量
            TemplateThreadLocal.setLayoutFile(layout.getLayoutFile());
        }
        return map;
    }
}
